﻿using LayuiMvc.DataEntity.DataEntity;
using LayuiMvc.Util.EnumUtil;
using LayuiMvc.DataEntity.DataEnum;
using LayuiMvc.Logic.Community;
using LayuiMvcUtil.Exception;
using LayuiMvcUtil.Result;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using LayuiMvc.DataModel.Community;
using LayuiMvc.Logic.LogicEnum;

namespace LayuiMvc.WebUI.Controllers
{
    public class PostController : Controller
    {
        /// <summary>
        /// 帖子管理业务逻辑类
        /// </summary>
        private PostService postService = new PostService();

        #region 帖子发布
        /// <summary>
        /// 帖子发表视图跳转
        /// </summary>
        /// <returns></returns>
        public ActionResult Publish()
        {
            List<ComboboxResult> postTypeList = EnumExtend.GetEnumDefineAttributes<PostTypeEnum>("PostType")
                .Select(m => new ComboboxResult()
                {
                    Value = m.Value,
                    Text = m.Description
                }).ToList();

            return View(postTypeList);
        }

        /// <summary>
        /// 发布帖子
        /// </summary>
        /// <param name="saveEntity">帖子实体类</param>
        /// <returns></returns>
        [ValidateInput(false)]
        [HttpPost]
        public async Task<ActionResult> Publish(PostEntity saveEntity)
        {
            DataResult<int> rtnResult = new DataResult<int>();
            try
            {
                saveEntity.PublishUserId = HttpContext.User.Identity.GetUserId<int>();
                saveEntity.PublishTime = DateTime.Now;

                int postId = await postService.PostAddSaveAsync(saveEntity);
                rtnResult.data = postId;
            }
            catch (CustomException exception)
            {
                rtnResult.code = 1;
                rtnResult.msg = exception.Message;
            }
            return Json(rtnResult);
        }
        #endregion

        #region 帖子详情
        /// <summary>
        /// 帖子详情
        /// </summary>
        /// <param name="postId">帖子Id</param>
        /// <param name="page">当前页</param>
        /// <param name="limit">每页记录</param>
        /// <returns></returns>
        public async Task<ActionResult> Detail(int postId, int page = 1, int limit = 20)
        {
            int userId = HttpContext.User.Identity.GetUserId<int>();
            DataResult<PostDetailModel> rtnResult = await postService.PostDetailAsync(postId, userId, page, limit);
            return View(rtnResult);
        }
        #endregion

        #region 帖子操作
        /// <summary>
        /// 帖子操作
        /// </summary>
        /// <param name="postId">帖子Id</param>
        /// <param name="operate">帖子操作</param>
        /// <returns></returns>
        public async Task<ActionResult> PostOperate(int postId, PostOperateEnum operate)
        {
            DataResult<string> rtnResult = new DataResult<string>();
            try
            {
                int userId = HttpContext.User.Identity.GetUserId<int>();
                await postService.PostOperateAsync(postId, userId, operate);
            }
            catch (CustomException exception)
            {
                rtnResult.code = 1;
                rtnResult.msg = exception.Message;
            }
            return Json(rtnResult);
        }
        #endregion

        #region 帖子收藏/取消收藏

        /// <summary>
        /// 收藏帖子
        /// </summary>
        /// <param name="postId">帖子ID</param>
        /// <returns></returns>
        public async Task<ActionResult> PostCollection(int postId)
        {
            DataResult<string> rtnResult = new DataResult<string>();

            try
            {
                int userId = HttpContext.User.Identity.GetUserId<int>();
                PostCollectionEntity collectionEntity = new PostCollectionEntity()
                {
                    PostId = postId,
                    CollectionUserId = userId,
                    CollectionTime = DateTime.Now
                };
                await postService.CollectionAddSaveAsync(collectionEntity);
            }
            catch (CustomException exception)
            {
                rtnResult.code = 1;
                rtnResult.msg = exception.Message;
            }
            return Json(rtnResult);
        }

        /// <summary>
        /// 取消帖子收藏
        /// </summary>
        /// <param name="postId">帖子ID</param>
        /// <returns></returns>
        public async Task<ActionResult> CollectionCancel(int postId)
        {
            DataResult<string> rtnResult = new DataResult<string>();
            try
            {
                int userId = HttpContext.User.Identity.GetUserId<int>();
                await postService.CollectionDeleteSaveAsync(userId, postId);
            }
            catch (CustomException exception)
            {
                rtnResult.code = 1;
                rtnResult.msg = exception.Message;
            }
            return Json(rtnResult);
        }
        #endregion

        #region 帖子列表
        /// <summary>
        /// 帖子分类列表
        /// </summary>
        /// <param name="postType">帖子列表</param>
        /// <returns></returns>
        public ActionResult PostType(int postType)
        {
            ViewBag.IsLogin = HttpContext.User.Identity.IsAuthenticated;
            ViewBag.PostType = postType;

            List<ComboboxResult> postTypeList = EnumExtend.GetEnumDefineAttributes<PostTypeEnum>("PostType")
                .Select(m => new ComboboxResult()
                {
                    Value = m.Value,
                    Text = m.Description
                }).ToList();
            return PartialView("_PostType", postTypeList);
        }
        #endregion
    }
}